package cn.edu.cqupt.cxcy.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cqupt.cxcy.dto.IndexImpl;
import cn.edu.cqupt.cxcy.util.Validate;


/**
 * 主页-【场地预约】
 * @author Dancer-mno
 * @date 2015年10月17日
 */
@SuppressWarnings("serial")
public class IndexAjaxAddZan extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		int id = Validate.stringIsNO(req.getParameter("id"))?Integer.parseInt(req.getParameter("id")) : 0; //类别
		// 主url  /index_zan
		int proid = Integer.valueOf(id);//项目id
		List<Object> pro = null;
		String error = null;
		if(id == 0) {
			error = "格式有误！";
		} else {
			pro = new IndexImpl().getProInfo(proid);
			if(pro == null || pro.isEmpty()) {
				error = "项目不存在！！";
			} else {
				if( new IndexImpl().addProZan(proid)) {//添加游览次数
					error = "点赞成功!";
				} else {
					error = "点赞失败!";
				}
			}
		}
		
		resp.setContentType("application/text;charset=utf-8");
		resp.setHeader("pragma","no-cache");
		resp.setHeader("cache-control","no-cache");
		PrintWriter out = resp.getWriter();
		out.print(error);
		out.flush();
		out.close();
	}


}
